﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class AlphaBlending : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// default construcotr
        /// </summary>
        public AlphaBlending()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_alphaBlending"></param>
        internal AlphaBlending(AlphaBlending _alphaBlending)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            return base.Execute(_bitmap);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = (UnsafeBitmap)_bitmap2.Clone();
            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    byte a1 = 0;
                    byte r1 = 0;
                    byte g1 = 0;
                    byte b1 = 0;

                    byte a2 = 0;
                    byte r2 = 0;
                    byte g2 = 0;
                    byte b2 = 0;

                    _bitmap1.GetPixelDirect(x,y, out a1, out r1, out g1, out b1);
                    _bitmap2.GetPixelDirect(x, y, out a2, out r2, out g2, out b2);

                    byte a = 0;
                    byte r = 0;
                    byte b = 0;
                    byte g = 0;

                    byte ar = 0;
                    int cb = ColorFunctions.ToIntRGB(r2, g2, b2);
                    int cs = ColorFunctions.ToIntRGB(r1, g1, b1);
                    int cr = 1 - (a1 / a2) / cb + a1 / ar * ((1 - a2) * cs + a2 * Blend(cb, cs));
                    _bitmap2.SetPixelDirect(x, y, Color.FromArgb(cr));
                }
            }
            return _bitmap2;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colorBackdrop"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private int Blend(int colorBackdrop, int color)
        {
            return -1;
        }
        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new AlphaBlending(this);
        }

    }
}
